<?php
/**
 * Created by PhpStorm.
 * User: xuxiaoming
 * Date: 2017/11/7
 * Time: 17:55
 */

namespace App\Http\Controllers;


use App\Models\Module;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller;

class ModuleController extends Controller
{
    /**
     * 列表
     * @return string
     */
    public function index()
    {
        $modules = Module::orderBy("sort",request()->get("sort","asc"))->get();
        if ($modules) {
            return sendJsonData($modules->toArray());
        } else{
            return sendJsonMessage('数据不存在',true);
        }
    }


    /**
     * 列表
     * @param Request $request
     * @return string
     */
    public function getModule(Request $request, $id)
    {
        $module = Module::find($id);
        if ($module) {
            return sendJsonData($module->toArray());
        } else{
            return sendJsonMessage('数据不存在',true);
        }
    }

    /**
     * 创建用户
     * @param Request $request
     * @return string
     */
    public function createModule(Request $request)
    {
        $modules = Module::create($request->all());
        return sendJsonData($modules->toArray());
    }

    /**
     * 更新用户
     *
     * @param Request $request
     * @param $id
     * @return string
     */
    public function updateModule(Request $request, $id)
    {
        $modules = Module::find($id);
        $modules->project_id = $request->input('project_id');
        $modules->title = $request->input('title');
        $modules->save();
        return sendJsonData($modules->toArray());
    }

    /**
     * 删除用户
     *
     * @param $id
     * @return string
     */
    public function deleteModule($id)
    {
        $modules = Module::find($id);
        if ($modules) {
            $modules->delete();
            return sendJsonMessage('删除成功');
        } else{
            return sendJsonMessage('数据不存在',true);
        }

    }


}